import payeeInfoPage from './payeeInfo/payeeInfo';
import storeInfoPage from './storeInfo/storeInfo';
const photoInfoPage = require('./photoInfo/photoInfo.js');
const photoPageData = new photoInfoPage.photoData();

import pickerView from '../../common/view/pickerview/pickerView';
import * as util from '../../utils/UmfUtils';
import location from '../../common/code/pickerList/location';
import busTypes from '../../common/code/pickerList/busTypes';
import TimeUtils from '../../utils/TimeUtils';
import * as Constants from '../../common/Constants';

const $swipertab = [
    {name: '收款人信息', index: 0, isCurr: false, marginleft: -10},
    {name: '店铺信息', index: 1, isCurr: false, marginleft: 2},
    {name: '上传证照', index: 2, isCurr: false, marginleft: -10},
    {name: '开通产品', index: 3, isCurr: false, marginleft: -10},];
var app = getApp();


var basisPage = {};

Page(Object.assign(
    {
        data: {
            // //swipertab
            // currtab: NaN,
            swipertab: $swipertab,
            //缓存数据
            cacheStorageData: {},
            //是否是企业
            isEnterprise: false,
            //上传照片页面数据
            photoData: {},
            storeId: '',
            cacheManager: null,
            locationData:null,
            //开通产品种类  1无pos 0有POS
            isPos:'0'
            // productAttribute: app.globalData.userData.productAttribute,
            
        },

        /**
         * 跳转参数options {
         *  storeId：店铺ID
         *  index：跳转到第几页
         *  paramsJson: 页面需要传的参数
         * }
         * @param options
         */
        onLoad: function (options) {
            basisPage = this;
            // 使用设备可视宽高
            wx.getSystemInfo({
                success: (res) => {
                    this.setData({
                        windowHeight: res.windowHeight
                    });
                }
            });

            let storeId = options.storeId;
            let paramsJson = options.paramsJson ? JSON.parse(options.paramsJson): {};
            paramsJson.storeId = storeId;
            this.setData({
                productAttribute:app.globalData.userData.productAttribute,
                storeId: storeId,
                today: TimeUtils.formatTime(),
                yesterday: TimeUtils.getLatestDate(1),
                storeAddress: options.storeAddress,
                isPos:options.isPos?options.isPos:"0"
            });
            //初始化pickerview
	        this._initPickerView();
            //this.initLinkagePicker([busTypes, location]);
            // this.initPage(index);
            //带参数跳转
            this.stepTo(options.index, paramsJson);
        },

        /**
         * 开通POS提示语
         * Q4POS接入
         */
        posTip: function () {
          wx.redirectTo({
            url: './openPos/openPos?storeId=' + this.data.storeId + '&posFrom=basis&storeAddress=' + this.data.storeAddress,
          }) 
        },
        imPayTip: function () {
          wx.redirectTo({
            url: './openImPay/openImPay?pageState=normal&storeId=' + this.data.storeId ,
          }) 

        },
        /**
         * 页面跳转，带参数
         * @param index
         * @param params
         */
        stepTo(index = 0, params) {
            switch (parseInt(index)) {
                case 0:
                    this.initPayeeInfoPage(this);
                    break;
                case 1:
                    this.initStoreInfoPage(this, params);
                    break;
                case 2:
                    let photoData = photoPageData.initPhotoInfoPage(this, params);
                    this.setData({
                        photoData: photoData
                    });
                    break;
                default:
            }

            //切换标签
            this.switchTabs(index);
        },

        /**
         * 切换标签
         * @param index
         */
        switchTabs(index = 0) {
            let swipertabs = this.data.swipertab;
            if (this.data.currtab) {
                swipertabs[this.data.currtab].isCurr = false;
            }
            swipertabs[index].isCurr = true;

            this.setData({
                currtab: index,
                swipertab: swipertabs
            })
        },
	
	    /**
         * [结算账单优化工单] 初始化pickerview
         * [立马付运营优化二期]
         * @private
         */
        _initPickerView() {
            //初始化pickerview
            let storeEmailList = app.globalData.agentCache.storeEmail;

            let labelList1 = util.getStorageSync(Constants.Keys.ANNO_LIST_1);
            let labelList2 = util.getStorageSync(Constants.Keys.ANNO_LIST_2);

            let normalList = [];
            // labelList1 && labelList1.length > 0 && normalList.push(this._convertAnnoList(labelList1));
            // labelList2 && labelList2.length > 0 && normalList.push(this._convertAnnoList(labelList2));
            // storeEmailList && storeEmailList.length > 0 && normalList.push(this._convertAnnoList(storeEmailList));

            normalList.push(labelList1 && labelList1.length > 0 ? this._convertAnnoList(labelList1) : { first: [] });
            normalList.push(labelList2 && labelList2.length > 0 ? this._convertAnnoList(labelList2) : { first: [] });
            normalList.push(storeEmailList && storeEmailList.length > 0 ? this._convertAnnoList(storeEmailList) : { first: [] });

            this.initLinkagePicker([busTypes, location]);
            normalList.length > 0 && this.initNormalPicker(normalList);
        },

        /**
         * [结算账单优化工单] 转化labelList
         * @param labelList
         * @returns {{first: Array}}
         * @private
         */
        _convertAnnoList(labelList){
            let maper = (item)=> {
                return {label: item};
            };

            return {first: labelList.map(maper)};
        },

        /**
         * 实时缓存
         * bindblur
         * @param e
         */
        realTimeCache: function (e) {
            let paramsKey = e.currentTarget.dataset.cachekey;
            this.data.cacheManager && this.data.cacheManager.cacheFunc({[paramsKey]: e.detail.value})
        },

        /**
         * 格式化身份证号
         * param:e
         */
        IDCardInput: function (e) {
            return util.formatIDCard(e.detail.value)
        },

        /**
         * 上传照片页面
         * @param ress
         */
        photoInfo_tapAdd: function (ress) {
            // UmfLog.log(ress);
            let id = ress.currentTarget.id;
            photoInfoPage.tapAdd(this, id, photoPageData);
        },

        photoInfo_tapPhoto: function (ress) {
            // UmfLog.log(ress);
            let id = ress.currentTarget.id;
            photoInfoPage.tapPhoto(this, id, photoPageData);
        },

        photoInfo_tapError: function (ress) {
            // UmfLog.log(ress);
            let id = ress.currentTarget.id;
            photoInfoPage.tapError(this, id, photoPageData);
        },

        photoInfo_tapExample: (ress) => {
            // UmfLog.log(ress);
            let id = ress.currentTarget.id;
            photoInfoPage.tapExample(id, photoPageData);
        },

        photoInfo_tapNext: function () {
            photoInfoPage.tapNext(photoPageData, () => {
                this.stepTo(3);
            });
        },

    }, payeeInfoPage, storeInfoPage, pickerView));

